package main

import (
	"fmt"
)

func lengthOfLastWord(s string) int {
	if len(s) == 0 {
		return 0
	}
	if s == " " {
		return 0
	}
	str := ""
	var strc []string
	//去掉开头和结尾的空格
	if s[0:1] == " " && s[len(s)-1:] != " " {
		str += s[1:]
	} else if s[0:1] != " " && s[len(s)-1:] == " " {
		str += s[0 : len(s)-1]
	} else if s[0:1] == " " && s[len(s)-1:] == " " {
		str += s[1 : len(s)-1]
		strc = append(strc, str)
	} else {
		str += s
	}
	var a []int
	for index, i := range str {
		if string(i) == " " {
			a = append(a, index)
		}
	}
	if len(a) == 0 {
		return len(str)
	} else {
		return len(str[a[len(a)-1]+1:])
	}
}

func main() {
	a := lengthOfLastWord("b   a    ")
	fmt.Println(a)
}
